package com.imy.lua.view.floating;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;

import com.imy.common.base.BaseCustomViewGroup;
import com.imy.common.manger.TaskManger;
import com.imy.common.util.ALog;
import com.imy.common.util.ToastUtil;
import com.imy.lua.LuaCore;
import com.imy.lua.R;
import com.imy.lua.activity.imagecolor.ImageColorToolsActivity;
import com.imy.lua.dialog.GuideImageColorDialog;
import com.imy.lua.helper.properties.PropertiesLoaderHelper;
import com.imy.lua.helper.screen.ScreenHelper;
import com.imy.lua.view.console.ConsoleView;

import java.util.Properties;

/**
 * @ProjectName: yuanshen
 * @Package: com.hykb.yuanshenmap.cloudgame.floating
 * @ClassName: QueueFloatingView
 * @Description: 描述
 * @Author: Imy
 * @CreateDate: 2021/4/9 16:23
 */
public class FloatingView extends BaseCustomViewGroup {
    private LinearLayout optionLl;
    private LinearLayout consoleLl;
    private ConsoleView consoleView;
    private LinearLayout imageLl;
    private boolean openOption = false;
    private LinearLayout nodeLl;

    public FloatingView(Context context) {
        super(context);
    }

    public FloatingView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FloatingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void init(View inflate) {
        View viewById = inflate.findViewById(R.id.floating_view);
        optionLl = inflate.findViewById(R.id.option_ll);
        nodeLl = inflate.findViewById(R.id.node_ll);
        consoleLl = inflate.findViewById(R.id.console_ll);
        consoleView = inflate.findViewById(R.id.console_view);
        imageLl = inflate.findViewById(R.id.image_ll);
        viewById.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (touchListener != null) {
                    touchListener.onTouch(event);
                }
                return true;
            }
        });
        consoleLl.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开控制台View
                consoleView.openOrClose();
            }
        });
        nodeLl.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean b = LuaCore.get().checkAccessibilityEnable();
                if (!b){
                    ToastUtil.toast(mContext,"无障碍未开启");
                    return;
                }
                setVisibility(GONE);

                //请求截屏 如果要把找图找色节点整合在一起的话 就需要放到无障碍下执行
                LuaCore.get().getAccessibilityServiceListener().logNode();
            }
        });
        imageLl.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean b = LuaCore.get().checkAccessibilityEnable();
                if (!b){
                    ToastUtil.toast(mContext,"无障碍未开启");
                    return;
                }
                Properties properties =
                        PropertiesLoaderHelper.getProperties();
                if (properties==null){
                    ToastUtil.toast(mContext,"使用图色调试工具，请先设置工程目录");
                    Activity topTask = TaskManger.get().getTopTask();
                    GuideImageColorDialog guideImageColorDialog = new GuideImageColorDialog((AppCompatActivity) topTask);
                    guideImageColorDialog.show();

                    return;
                }
                setVisibility(GONE);
                //隐藏
                ScreenHelper.get().startFastScreen(new ScreenHelper.OnScreenCaptureListener() {
                    @Override
                    public void onSuccess(String path) {
                        ALog.i(TAG,"startFastScreen:"+path);
                        ImageColorToolsActivity.start(mContext,path);
                    }

                    @Override
                    public void onFail() {
                        setVisibility(VISIBLE);
                    }
                });
            }
        });
    }

    public void closeChildView() {
        optionLl.setVisibility(GONE);
        openOption = false;
        consoleView.hide();
    }

    public void openOptionOrClose() {
        if (openOption) {
            optionLl.setVisibility(GONE);
            openOption = false;
        } else {
            optionLl.setVisibility(VISIBLE);
            openOption = true;
        }
    }

    @Override
    protected int getLayoutID() {
        return R.layout.custom_view_queue_floating;
    }

    public ConsoleView getConsoleView() {
        return consoleView;
    }

    public interface TouchListener {
        void onTouch(MotionEvent event);
    }

    public void setTouchListener(TouchListener touchListener) {
        this.touchListener = touchListener;
    }

    private TouchListener touchListener;
}